package com.ag.lwc;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.ag.bean.TbEvent;
import com.ag.bean.TbUser;
import com.ag.dao.TbEventDAO;
import com.ag.dao.TbUserDAO;

/**
 * 清空用户表的例程
 * @author user
 *
 */
public class ClearTable {

	/**
	 * @param args
	 */
	public static ApplicationContext context = new FileSystemXmlApplicationContext(new String[]{ "classpath:/applicationContext.xml",
			"classpath:/applicationContext-dgm.xml", 
			"classpath:/applicationContext-hmk.xml",
			"classpath:/applicationContext-lzw.xml",
			"classpath:/applicationContext-lwc.xml",
			"classpath:/applicationContext-lzm.xml",
			"classpath:/applicationContext-wsq.xml",
			"classpath:/applicationContext-wzp.xml",
			"classpath:/applicationContext-zhh.xml",
			"classpath:/applicationContext-zlw.xml",
			"classpath:/applicationContext-zrt.xml",});
	public static TbUserDAO userdao = (TbUserDAO) context.getBean("TbUserDAO");
	public static TbEventDAO eventdao = (TbEventDAO)context.getBean("TbEventDAO");
	public void clearEvent()
	{
		Session session = eventdao.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();
		List<TbEvent> events = session.createQuery("from TbEvent").list();
		for(TbEvent event:events)
		{
			System.out.println(event.getEEventname());
			session.delete(event);
		}
		tx.commit();
		session.close();
	}
	public void clearUser()
	{
		List<TbUser> users = userdao.findAll();	
		for(TbUser user:users)
		{
			userdao.delete(user);
		}
	}
	public static void main(String[] args) {
		ClearTable test = new ClearTable();
	//	test.clearUser();
		test.clearEvent();

	}

}
